<?php

/*

  Action - Attack	$5

  +3 Cards
  Each other player reveals the top 3 cards of his deck, discards the revealed Actions and Treasures, and puts the rest back on top in any order he chooses.

 */

class Rabble extends Card implements iAction, iKingdom, iAttack, iProsperity {

    public function __construct() {
        parent::__construct("Rabble", 5);
        $this->undoPlay = false;
        $this->undoBuy = true;
    }

    public function getDescription() {
        return array(
            "+3 Cards",
            "Each other player reveals the top 3 cards of his deck, discards the revealed Actions and Treasures, and puts the rest back on top in any order he chooses.");
    }
    
    public function play(Game $game, $isRealCard) {

        $drawed_cards = $game->drawCards(3);
         ScreenDisplay::addScreenMessage($game, true, "draw " . nicePrint($drawed_cards), "draws " . sizeof($drawed_cards) . "cards");
        $game->setPhase(Phase_Sleep);
        $game->attack($this);

        $names = $game->getOtherPlayersNames(true);
        foreach ($names as $name) {
            $cards = $game->removeCardsFromDrawDeck(3, $name);
            $discardCards = array();
            $victoryCards = array();
            foreach ($cards as $card) {
                if ($card instanceof iVictory) {
                    $victoryCards[] = $card;
                } else {
                    $discardCards[] = $card;
                }
            }
            
            $revealed_cards = array_merge($victoryCards, $discardCards);
             ScreenDisplay::addScreenMessage($game, true, "reveal " . nicePrint($revealed_cards) , "reveals ".ScreenDisplay::$LMSG, $name);
            $game->discardTempDiscardCards($discardCards, $name);
            if (sizeof($victoryCards) > 0) {

                $output = MySocket::GetUserReply($game, Input_Reorder, $victoryCards, -1, -1, null, "Choose the order of the cards", $name, true);
            }
        }

        //the waiting function
        $users_replies = MySocket::getUsersReplies($game);
        foreach ($users_replies as $name => $output) {
             ScreenDisplay::addScreenMessage($game, true, "put " . nicePrint($output)." on top of your deck and discard the rest" , "puts ".sizeof($output)." cards of top of his deck and discards the rest", $name);
             $output = array_reverse($output);
            foreach ($output as $card_name) {
                $game->putCardOnTopOfDrawDeck($card_name, $name);
                
            }

            $game->setPhase(Phase_Sleep, $name);
        }

        $game->cardActionFinished();
        return true;
    }

}

?>